<?php

$path = @$_REQUEST['path'];
$w = intval(@$_REQUEST['w']);
$h = intval(@$_REQUEST['h']);
$c = @$_REQUEST['c'];
$t = @$_REQUEST['t'];

if (!$path || !$w || !$h) return;

include('../tinymvc/myfiles/plugins/tinymvc_script_imageutil.php');
$root = dirname(__FILE__);
$source = "$root/uploads/$path.".$t;
$image = "$root/uploads/$path-{$w}x{$h}{$c}.".$t;

if ($c) {
  $ret = Imageutil::cropImage($source, $image, $w, $h);
} else {
  $ret = Imageutil::convertImage($source, $image, $w.'x'.$h);
}
if ($ret) {

  if (!file_exists($image)) {
    header("HTTP/1.0 404 Not Found");
    exit();
  }

  $fsize = filesize($image);
  $expires = 60*60*24*4*3;
  header('Content-Type: image/jpeg');
  header("Pragma: public");
  header("Cache-Control: max-age=".$expires.", public, must-revalidate");
  header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
  header('Content-Length: '.$fsize);
  $file = @fopen($image,"rb");
  if ($file) {
    while(!feof($file)) {
      echo(fread($file, 2*1024));
      flush();
      if (connection_status()!=0) {
        @fclose($file);
        return  false;
      }
    }
    @fclose($file);
  }
}
